package com.sist.ocs.component;

import com.sist.ocs.model.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @ClassName MyConfirmCallbackSender
 * @Description TODO
 * @Author: xmk
 * @Date: 2018/10/24 17:00
 * @Version: 1.0
 */
@Component
public class MyConfirmCallbackSender implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        User user = new User();
        user.setPassword("123123");
        user.setUsername("xxx");

        rabbitTemplate.setConfirmCallback(this);

        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        System.out.println(correlationData.getId());

        rabbitTemplate.convertAndSend("mydirectExchage", "myDirectConfQue", user, correlationData);
    }

    // ConfirmCallback接口用于实现消息发送到RabbitMQ交换器后接收ack回调。
    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        System.out.println("id : " + correlationData.getId());
        System.out.println("b : " + b);
        System.out.println("s : " + s);
    }
}
